package tech.wetech.admin3.controller;

import jakarta.validation.constraints.NotBlank;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import tech.wetech.admin3.sys.model.AooUser;
import tech.wetech.admin3.sys.service.AooCompCertiDataService;
import tech.wetech.admin3.sys.service.AooPersCertiDataService;
import tech.wetech.admin3.sys.service.AooPhoneCodeService;
import tech.wetech.admin3.sys.service.AooUserService;
import tech.wetech.admin3.util.CustomResponse;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/aoo")
public class AooUserController {
  private AooUserService aooUserService;
  private AooPersCertiDataService persCertiDataService;
  private AooCompCertiDataService compCertiDataService;
  private AooPhoneCodeService phoneCodeService;

  public AooUserController(
    AooUserService aooUserService,
    AooPersCertiDataService persCertiDataService,
    AooCompCertiDataService compCertiDataService,
    AooPhoneCodeService phoneCodeService
  ) {
    this.aooUserService = aooUserService;
    this.persCertiDataService = persCertiDataService;
    this.compCertiDataService = compCertiDataService;
    this.phoneCodeService = phoneCodeService;
  }

  //   创建新用户
  @PostMapping("/register")
  public ResponseEntity<?> register(@RequestBody RegisterRequest registerRequest) {
    AooUser userEmail = aooUserService.findByEmail(registerRequest.email);
    if (userEmail != null) {
      return ResponseEntity.ok(new CustomResponse.r1<>("该邮箱已注册", false, ""));
    } else {
      AooUser userPhone = aooUserService.findByPhone(registerRequest.phone);
      if (userPhone != null) {
        return ResponseEntity.ok(new CustomResponse.r1("该手机号已注册", false, ""));
      }
    }

    boolean flag = phoneCodeService.verifyCode(registerRequest.phone, registerRequest.vericode);
    if (!flag) {
      return ResponseEntity.ok(new CustomResponse.r1("验证码错误或已过期", false, ""));
    }

    AooUser aooUser = new AooUser();
    aooUser.setEmail(registerRequest.email);
    aooUser.setPassword(registerRequest.password);
    aooUser.setPhone(registerRequest.phone);

    aooUserService.register(aooUser);
    AooUser user = aooUserService.findByEmail(registerRequest.email);
    if (user == null) {
      return ResponseEntity.ok(new CustomResponse.r1("注册失败，请重试", false, null));
    }
    return ResponseEntity.ok(new CustomResponse.r1("注册成功", true, user));
  }

//  @PostMapping("/register2")
//  public ResponseEntity<?> register2(
//    @RequestBody RegisterRequest2 registerRequest2
//  ) {
//    AooUser userEmail = aooUserService.findByEmail(registerRequest2.email);
//    if (userEmail != null) {
//      return ResponseEntity.ok(new CustomResponse.r1<>("该邮箱已注册", false, ""));
//    }
//
//    AooUser aooUser = new AooUser();
//    aooUser.setEmail(registerRequest2.email);
//    aooUser.setPassword(registerRequest2.password);
//
//    aooUserService.register(aooUser);
//    AooUser user = aooUserService.findByEmail(registerRequest2.email);
//    if (user == null) {
//      return ResponseEntity.ok(new CustomResponse.r1("注册失败，请重试", false, null));
//    }
//    return ResponseEntity.ok(new CustomResponse.r1("注册成功", true, user));
//  }



  @PutMapping("/updateRole")
  public ResponseEntity<?> updateRole(
    @RequestBody UpdateRoleRequest updateRoleRequest,
    @RequestParam("vericode") String vericode
  ){
    boolean flag_admin = phoneCodeService.admin_VerifyCode(vericode);
    if(!flag_admin){
      throw new IllegalArgumentException("admin 验证码错误或已过期");
    }

    String email = updateRoleRequest.email;
    String newRole = updateRoleRequest.newRole;

    boolean flag = aooUserService.updateRole(email, newRole);

    if (flag) {
      return ResponseEntity.ok(new CustomResponse.r2("角色已修改", 1000,null));
    } else {
      return ResponseEntity.ok(new CustomResponse.r2("用户不存在", 1001,null));
    }
  }

  @GetMapping("/getusers")
  public ResponseEntity<?> getAllUser() {
    List data = aooUserService.getAllUser();
    return ResponseEntity.ok(new CustomResponse.r2("获取用户列表", 1000, data));
  }

  @GetMapping("/get-authdata")
  public ResponseEntity<?> getAuthData(String email) {
    AooUser user = aooUserService.findByEmail(email);
    Integer authType = user.getAuthType();
    if (authType == 0) {
      return ResponseEntity.ok(new CustomResponse.r2("未认证", 1003, null));
    } else if (authType == 1) {
      return ResponseEntity.ok(new CustomResponse.r2("个人认证", 1001, persCertiDataService.findByEmail(email)));
    } else {
      return ResponseEntity.ok(new CustomResponse.r2("企业认证", 1002, compCertiDataService.findByEmailAndStatus(email, 2)));
    }
  }

  @Transactional
  @DeleteMapping("user/admin/delete")
  public ResponseEntity<String> deleteUser(
    @RequestParam("email") String email,
    @RequestParam("vericode") String vericode
  ) {
    boolean flag = phoneCodeService.admin_VerifyCode(vericode);
    if(!flag){
      throw new IllegalArgumentException("admin 验证码错误或已过期");
    }

    try {
      aooUserService.deleteUser(email);
      // 检查并删除个人认证数据
      if (persCertiDataService.findByEmail(email) != null) {
        persCertiDataService.deleteByEmail(email);
      }

      // 检查并删除企业认证数据
      if (compCertiDataService.findByEmail(email) != null) {
        compCertiDataService.deleteByEmail(email);
      }

      return ResponseEntity.ok("已删除用户:" + email);
    } catch (AooUserService.UserNotFoundException e) {
      return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
    }
  }

  @PutMapping("/updateInfo")
  public String update(
    @RequestParam("email") String email,
    @RequestParam("key") String key,
    @RequestParam("value") String value
  ) {
    aooUserService.update(email, key, value);
    return "更新成功";
  }

  @PutMapping("/admin/updateInfo")
  public AooUser admin_update(
    @RequestParam("email") String email,
    @RequestParam("key") String key,
    @RequestParam("value") Integer value,
    @RequestParam("vericode") String vericode
  ) {
    return aooUserService.admin_update(email, key, value, vericode);
  }

  record RegisterRequest(
    @NotBlank String email,
    @NotBlank String password,
    @NotBlank String phone,
    @NotBlank String vericode) {
  }

  record RegisterRequest2(
    @NotBlank String email,
    @NotBlank String password
  ) {
  }


  record ResetPwByEmailRequest(
    @NotBlank String email,
    @NotBlank String password
  ) {
  }

  record UpdateRoleRequest(
    @NotBlank String email,
    @NotBlank String newRole
  ) {
  }

}
